source /etc/profile
source ~/.bash_profile 
### 微服务部署目录
MADP_MICRO_DIR=~/madp/madp-cp-micro
### 微服务JAR常量定义
REGISTER=madp-register.jar
AUTH=madp-auth.jar
UPMS=madp-upms-biz.jar
QUARTZ=madp-daemon-quartz.jar
CP=madp-cp-biz.jar
GATEWAY=madp-gateway.jar
### 微服务端口常量
REGISTER_PORT=8848
AUTH_PORT=3000
UPMS_PORT=4000
QUARTZ_PORT=5010
CP_PORT=9500
GATEWAY_PORT=9999
#######################################################################################


#######################################################################################
######################################0.1 停止方法定义###################################
### 0.1.1 停止注册中心
stopRegister()
{
    REGISTER_PID=`ps -ef | grep -w $REGISTER | grep -v "grep" | awk '{print $2}'`
    if [ "$REGISTER_PID" == "" ]; then
        echo "[MADP] [REGISTER]进程不存在或已停止成功---------------------------------------------"
    else
        kill -9 $REGISTER_PID
        echo "[MADP] [REGISTER]停止成功---------------------------------------------------------"
    fi
}

### 0.1.2 停止认证中心
stopAuth()
{
    AUTH_PID=`ps -ef | grep -w $AUTH | grep -v "grep" | awk '{print $2}'`
    if [ "$AUTH_PID" == "" ]; then
        echo "[MADP] [AUTH]进程不存在或已停止成功------------------------------------------------"
    else
        kill -9 $AUTH_PID
        echo "[MADP] [AUTH]停止成功------------------------------------------------------------"
    fi
}

### 0.1.3 停止权限管理
stopUpms()
{
    USPM_PID=`ps -ef | grep -w $UPMS | grep -v "grep" | awk '{print $2}'`
    if [ "$USPM_PID" == "" ]; then
        echo "[MADP] [UPMS]进程不存在或已停止成功------------------------------------------------"
    else
        kill -9 $USPM_PID
        echo "[MADP] [UPMS]停止成功------------------------------------------------------------"
    fi
}

### 0.1.4 停止定时任务
stopQuartz()
{
    QUARTZ_PID=`ps -ef | grep -w $QUARTZ | grep -v "grep" | awk '{print $2}'`
    if [ "$QUARTZ_PID" == "" ]; then
        echo "[MADP] [QUARTZ]进程不存在或已停止成功-----------------------------------------------"
    else
        kill -9 $QUARTZ_PID
        echo "[MADP] [QUARTZ]停止成功-----------------------------------------------------------"
    fi
}

### 0.1.5 停止企业微信
stopCp()
{
    CP_PID=`ps -ef | grep -w $CP | grep -v "grep" | awk '{print $2}'`
    if [ "$CP_PID" == "" ]; then
        echo "[MADP] [CP]进程不存在或已停止成功--------------------------------------------------"
    else
        kill -9 $CP_PID
        echo "[MADP] [CP]停止成功--------------------------------------------------------------"
    fi
}

### 0.1.6 停止网关
stopGateway()
{
    GATEWAY_PID=`ps -ef | grep -w $GATEWAY | grep -v "grep" | awk '{print $2}'`
    if [ "$GATEWAY_PID" == "" ]; then
        echo "[MADP] [GATEWAY]进程不存在或已停止成功-----------------------------------------------"
    else
        kill -9 $GATEWAY_PID
        echo "[MADP] [GATEWAY]停止成功-----------------------------------------------------------"
    fi
}


#######################################################################################
######################################0.2 启动方法定义###################################

### 0.2.1 启动注册中心
startRegister()
{
    cd $MADP_MICRO_DIR
    echo "[MADP] [REGISTER]准备启动---------------------------------------------------------"
    nohup java -Xms256m -Xmx256m -XX:MaxMetaspaceSize=512m -Dspring.profiles.active=native -Duser.timezone=GMT+8 -jar $REGISTER > $MADP_MICRO_DIR/logs/run.log 2>&1 &
    REGISTER_PID=`lsof -i:$REGISTER_PORT|grep "LISTEN"|awk '{print $2}'`
    until [ -n "$REGISTER_PID" ]
        do
            REGISTER_PID=`lsof -i:$REGISTER_PORT|grep "LISTEN"|awk '{print $2}'`
        done
    echo "[MADP] [REGISTER] PID: $REGISTER_PID"
    echo "[MADP] [REGISTER]成功启动---------------------------------------------------------"
}

### 0.2.2 启动认证中心
startAuth()
{
    cd $MADP_MICRO_DIR
    echo "[MADP] [AUTH]准备启动-----------------------------------------------------------"
    nohup java -Xms256m -Xmx256m -XX:MaxMetaspaceSize=512m -Duser.timezone=GMT+8 -jar $AUTH > $MADP_MICRO_DIR/logs/run.log 2>&1 &
    AUTH_PID=`lsof -i:$AUTH_PORT|grep "LISTEN"|awk '{print $2}'`
    until [ -n "$AUTH_PID" ]
        do
            AUTH_PID=`lsof -i:$AUTH_PORT|grep "LISTEN"|awk '{print $2}'`
        done
    echo "[MADP] [AUTH] PID: $AUTH_PID"
    echo "[MADP] [AUTH]成功启动-----------------------------------------------------------"
}

### 0.2.3 启动权限管理
startUpms()
{
    cd $MADP_MICRO_DIR
    echo "[MADP] [UPMS]准备启动-----------------------------------------------------------"
    nohup java -Xms256m -Xmx256m -XX:MaxMetaspaceSize=512m -Duser.timezone=GMT+8 -jar $UPMS > $MADP_MICRO_DIR/logs/run.log 2>&1 &
    UPMS_PID=`lsof -i:$UPMS_PORT|grep "LISTEN"|awk '{print $2}'`
    until [ -n "$UPMS_PID" ]
        do
            UPMS_PID=`lsof -i:$UPMS_PORT|grep "LISTEN"|awk '{print $2}'`
        done
    echo "[MADP] [UPMS] PID: $UPMS_PID"
    echo "[MADP] [UPMS]成功启动-----------------------------------------------------------"
}

### 0.2.4 启动定时任务
startQuartz()
{
    cd $MADP_MICRO_DIR
    echo "[MADP] [QUARTZ]准备启动--------------------------------------------------------"
    nohup java -Xms256m -Xmx256m -XX:MaxMetaspaceSize=512m -Duser.timezone=GMT+8 -jar $QUARTZ > $MADP_MICRO_DIR/logs/run.log 2>&1 &
    QUARTZ_PID=`lsof -i:$QUARTZ_PORT|grep "LISTEN"|awk '{print $2}'`
    until [ -n "$QUARTZ_PID" ]
        do
            QUARTZ_PID=`lsof -i:$QUARTZ_PORT|grep "LISTEN"|awk '{print $2}'`
        done
    echo "[MADP] [QUARTZ] PID: $QUARTZ_PID"
    echo "[MADP] [QUARTZ]成功启动--------------------------------------------------------"
}

### 0.2.5 启动企业微信
startCp()
{
    cd $MADP_MICRO_DIR
    echo "[MADP] [CP]准备启动---------------------------------------------------------"
    nohup java -Xms1024m -Xmx1024m -XX:MaxMetaspaceSize=256m -Duser.timezone=GMT+8 -jar $CP > $MADP_MICRO_DIR/logs/run.log 2>&1 &
    CP_PID=`lsof -i:$CP_PORT|grep "LISTEN"|awk '{print $2}'`
    until [ -n "$CP_PID" ]
        do
            CP_PID=`lsof -i:$CP_PORT|grep "LISTEN"|awk '{print $2}'`
        done
    echo "[MADP] [CP] PID: $CP_PID"
    echo "[MADP] [CP]成功启动---------------------------------------------------------"
}

### 0.2.6 启动网关
startGateway()
{
    cd $MADP_MICRO_DIR
    echo "[MADP] [GATEWAY]准备启动--------------------------------------------------------"
    nohup java -Xms256m -Xmx256m -XX:MaxMetaspaceSize=512m -Duser.timezone=GMT+8 -jar $GATEWAY > $MADP_MICRO_DIR/logs/run.log 2>&1 &
    GATEWAY_PID=`lsof -i:$GATEWAY_PORT|grep "LISTEN"|awk '{print $2}'`
    until [ -n "$GATEWAY_PID" ]
        do
            GATEWAY_PID=`lsof -i:$GATEWAY_PORT|grep "LISTEN"|awk '{print $2}'`
        done
    echo "[MADP] [GATEWAY] PID: $GATEWAY_PID"
    echo "[MADP] [GATEWAY]成功启动--------------------------------------------------------"
}

#######################################################################################
######################################0.3 替换方法定义###################################

### 0.3.1 替换注册中心JAR
replaceRegister()
{
    echo "[MADP] [REGISTER]替换开始-------------------------------------------------"
    rm -rf $MADP_MICRO_DIR/$REGISTER
    cp $PROJ_PATH/madp-register/target/$REGISTER $MADP_MICRO_DIR
    echo "[MADP] [REGISTER]替换结束-------------------------------------------------"
}

### 0.3.2 替换认证中心JAR
replaceAuth()
{
    echo "[MADP] [AUTH]替换开始-------------------------------------------------"
    rm -rf $MADP_MICRO_DIR/$AUTH
    cp $PROJ_PATH/madp-auth/target/$AUTH $MADP_MICRO_DIR
    echo "[MADP] [AUTH]替换结束-------------------------------------------------"
}

### 0.3.3 替换权限管理JAR
replaceUpms()
{
    echo "[MADP] [UPMS]替换开始-------------------------------------------------"
    rm -rf $MADP_MICRO_DIR/$UPMS
    cp $PROJ_PATH/madp-modules/madp-upms-biz/target/$UPMS $MADP_MICRO_DIR
    echo "[MADP] [UPMS]替换结束-------------------------------------------------"
}

### 0.3.4 替换定时任务JAR
replaceQuartz()
{
    echo "[MADP] [QUARTZ]替换开始-------------------------------------------------"
    rm -rf $MADP_MICRO_DIR/$QUARTZ
    cp $PROJ_PATH/madp-visual/madp-daemon-quartz/target/$QUARTZ $MADP_MICRO_DIR
    echo "[MADP] [QUARTZ]替换结束-------------------------------------------------"
}

### 0.3.5 企业微信JAR
replaceCp()
{
    echo "[MADP] [CP]替换开始-------------------------------------------------"
    rm -rf $MADP_MICRO_DIR/$CP
    cp $PROJ_PATH/madp-modules/madp-cp-biz/target/$CP $MADP_MICRO_DIR
    echo "[MADP] [CP]替换结束-------------------------------------------------"
}

### 0.3.6 替换网关JAR
replaceGateway()
{
    echo "[MADP] [GATEWAY]替换开始-------------------------------------------------"
    rm -rf $MADP_MICRO_DIR/$GATEWAY
    cp $PROJ_PATH/madp-gateway/target/$GATEWAY $MADP_MICRO_DIR
    echo "[MADP] [GATEWAY]替换结束-------------------------------------------------"
}



#######################################################################################
#######################################################################################

### 0.4.1 编译工程
buildMadp()
{
    cd $PROJ_PATH/
    mvn clean install -Dmaven.test.skip=true
}

### 0.4.2 确保注册中心成功注册
sleepMADP()
{
    echo "[MADP] [SELLP]准备开始----------------------------------------------------------"
    sleep 1
    echo "[MADP] [确]"
    sleep 1
    echo "[MADP] [保]"
    sleep 1
    echo "[MADP] [注]"
    sleep 1
    echo "[MADP] [册]"
    sleep 1
    echo "[MADP] [中]"
    sleep 1
    echo "[MADP] [心]"
    sleep 1
    echo "[MADP] [注]"
    sleep 1
    echo "[MADP] [册]"
    sleep 1
    echo "[MADP] [成]"
    sleep 1
    echo "[MADP] [功]"
    echo "[MADP] [SELLP]完全结束----------------------------------------------------------"
}

#######################################################################################
#######################################################################################

# 0.5.1 单独重启网关
restartGateway()
{
    # ①停止服务
    stopGateway
    # ②重启服务
    startGateway
}

# 0.5.2 单独重启权限管理
restartUpms()
{
    # ①编译工程
    buildMadp
    # ②停止服务
    stopUpms
    # ③替换服务
    replaceUpms
    # ④重启服务
    startUpms
    # ⑤重启网关
    #restartGateway
}

# 0.5.3 单独重启注册中心
restartNacos()
{
    # ①编译工程
    buildMadp
    # ②停止服务
    stopRegister
    # ③替换服务
    replaceRegister
    # ④重启服务
    startRegister
    # ⑤重启网关
    #restartGateway
}

# 0.5.8 单独重启定时任务
restartQuartz()
{
    # ①编译工程
    buildMadp
    # ②停止服务
    stopQuartz
    # ③替换服务
    replaceQuartz
    # ④重启服务
    startQuartz
    # ⑤重启网关
    #restartGateway
}

# 0.5.12 单独重启企业微信
restartCp()
{
    # ①编译工程
    buildMadp
    # ②停止服务
    stopCp
    # ③替换服务
    replaceCp
    # ④重启服务
    startCp
}

#######################################################################################
#######################################################################################

### 1 停止微服务
stopMADP()
{
    # 1、GATEWAY
    stopGateway
    # 2、CP
    stopCp
    # 3、UPMS
    stopUpms
    # 4、AUTH
    stopAuth
    # 5、QUARTZ
    stopQuartz
    # 6、REGISTER
    stopRegister
}

### 2 替换新版本微服务JAR
replaceMADP()
{
    # 1、GATEWAY
    replaceGateway
	  # 2、CP
    replaceCp
    # 3、UPMS
    replaceUpms
    # 4、AUTH
    replaceAuth
    # 5、QUARTZ
    replaceQuartz
    # 6、REGISTER
    replaceRegister
}


### 3 启动微服务
startMADP()
{
    # 1、REGISTER
    startRegister
    # 0、SLEEP
    sleepMADP
    # 2、AUTH
    startAuth
    # 3、UPMS
    startUpms
  	# 4、CP
    startCp
    # 5、QUARTZ
    startQuartz
    # 6、GATEWAY
    startGateway
}

### 4 重启微服务
restartMADP()
{
    # ①编译工程
    buildMadp
    # ②停止服务
    stopMADP
    # ③替换服务
    replaceMADP
    # ④启动服务
    startMADP
}

#######################################################################################

case "$1" in
    start)
        startMADP
        ;;
    stop)
        stopMADP
        ;;
    restart)
        restartMADP
        ;;
    nacos)
        restartNacos
        ;;
    gateway)
        restartGateway
        ;;
    upms)
        restartUpms
        ;;
     quartz)
        restartQuartz
        ;;
    cp)
        restartCp
        ;;
esac
exit 0

